package com.zrrd.yunchmall.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.user.entity.*;
import com.zrrd.yunchmall.user.service.IRoleMenuRelationService;
import com.zrrd.yunchmall.user.service.IRoleService;
import com.zrrd.yunchmall.user.service.impl.RoleServiceImpl;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 后台用户角色表 前端控制器
 * </p>
 *
 * @author YeKong
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/role")
@Api(tags = "权限服务-角色模块")
public class RoleController {

    @Autowired
    private IRoleService roleService;


    @ApiOperation("查询所有角色信息")
    @GetMapping("/listAll")
    public ResponseResult listAll() {
        return new ResponseResult(200, "成功查询到所有的角色", roleService.list());
    }

    @ApiOperation("查询角色列表")
    @GetMapping("/list")
    public ResponseResult list(@ApiParam(required = true, defaultValue = "1") @RequestParam int pageSize,
                               @ApiParam(required = true, defaultValue = "10") @RequestParam int pageNum,
                               @ApiParam String keyword){
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)){//当关键字不为空
            //where username = ? or nick_name = keyword
            queryWrapper.eq("name", keyword); //username为数据表中属性名，必须一致
        }
        //分页操作
        IPage<Role> pageInfo = roleService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return new ResponseResult<>(200,"查询成功",pageInfo);
    }


    @ApiOperation("添加一个角色")
    @PostMapping("/create")
    public ResponseResult create(@RequestBody Role role){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", role.getName());
        if (roleService.count(queryWrapper) > 0) {
            return new ResponseResult(205,"注册失败，用户名已存在");
        }
        queryWrapper.eq("description", role.getDescription());
        //设置创建的时间
        role.setCreateTime(LocalDateTime.now());
        //在数据库中保存管理员对象
        roleService.save(role);
        return new ResponseResult(200,"注册成功");
    }

    @ApiOperation("修改角色信息")
    @PostMapping("/update/{id}")
    public ResponseResult update(@RequestBody Role role, @PathVariable("id") long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        roleService.update(role,updateWrapper);
        return new ResponseResult(200,"修改成功");
    }

    @ApiOperation("删除角色账号")
    @PostMapping("/delete")
    public ResponseResult delete(String ids) {
//        roleService.removeById(id);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids.split(","));
        roleService.remove(queryWrapper);
        return new ResponseResult(200,"删除成功");
    }

    @ApiOperation("修改是否启用")
    @PostMapping("/updateStatus/{id}")
    public ResponseResult updateStatus(@PathVariable("id") long id, @RequestParam int status) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        updateWrapper.set("status", status);
        roleService.update(updateWrapper);
        return new ResponseResult(200,"修改成功");
    }

    @ApiOperation("查询角色现有菜单")
    @GetMapping("/listMenu/{roleId}")
    public ResponseResult menu(@PathVariable("roleId") long roleId) {
        List<Menu> menus = roleService.getmenulist(roleId);
        return new ResponseResult(200,"查询成功",menus);
    }

    @ApiOperation("查询角色现有资源")
    @GetMapping("/listResource/{roleId}")
    public ResponseResult resource(@PathVariable("roleId") long roleId) {
        List<Resource> resources = roleService.getresourcelist(roleId);
        return new ResponseResult(200,"查询成功",resources);
    }



    @ApiOperation("分配菜单")
    @PostMapping("/allocMenu")
    public ResponseResult allocRole(@RequestParam long roleId, @RequestParam String menuIds) {
        roleService.allocRole(roleId, menuIds);
        return new ResponseResult<>(200, "修改成功");
    }

    @ApiOperation("分配资源")
    @PostMapping("/allocResource")
    public ResponseResult allocResource(@RequestParam long roleId, @RequestParam String resourceIds) {
        roleService.allocResouce(roleId, resourceIds);
        return new ResponseResult<>(200, "修改成功");
    }


}
